#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>

#include "BasicTypes.h"

#include "Utils.h"

bool MemoryAssert(int num, ...)
{
  void* p;
  int iter;
  va_list list;
  bool toDelete = FALSE;
  
  bool hasFile = num <= 0;
  if (hasFile)
	  num = -num;
  
  va_start(list, num);
  
  for (iter = 0; iter < num; iter++)
  {
    p = va_arg(list, void*);
    if (p == NULL)
	{
		toDelete = TRUE;
		break;
	}
  }

  if (toDelete)
  {
    va_end(list);
	va_start(list, num);
	for (iter = 0; iter < num; iter++)
	{
		p = va_arg(list, void*);
		free(p);
	}
	if (hasFile && (fclose(va_arg(list, void*)) == EOF))
		perror("Error on closing file, in memory assert.");
  }

  va_end(list);

  return !toDelete;
}
